### Replication code for Taylor C. Boas, F. Daniel Hidalgo, and Guillermo Toral. "Competence versus Priorities: Negative Electoral Responses to Education Quality in Brazil"
### This file replicates the analyses of the online survey experiment included in the main text and in the Appendices
### R version, platform, and package versions reported at the end of the file
### June 19, 2020

# PREPARE THE ENVIRONMENT -------------------------------------------------
# Set Working Directory to wherever this file is located.

# The directory where this file is located must also have a "figures" and a "tables" subdirectory
# Clean the environment
rm(list = ls())
# Load packages (make sure they are previously installed)
library(tidyverse); library(multcomp); library(xtable); library(texreg); library(Matching); library(ebal); library(cobalt); library(lmtest); library(codebook)

# IMPORT CLEANED DATASET -------------------------------------------------
# Import data as assembled by online_survey_dataset.R
dd <- read_csv("data/online_survey_cleaned.csv")
# De-mean covariates that will be used as controls, as per the pre-analysis plan
dd <- dd %>% 
  mutate_each(funs(.-mean(.,na.rm=T)), c("age", "female", "education", "race_white", "race_brown", "race_other","region_north","region_centerwest","region_southeast","region_south", "race_white", "race_brown", "race_other"))

# FIGURE 4: CONDITIONAL AVERAGE TREATMENT EFFECTS BY PRIORITY GIVEN TO EDUCATION ----------------------------------------------------------------
# Regressions for positive treatment, heterogeneity by priority given to education (Table C.22)
m1 <- lm(mayor_invested_education~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m1_low <- summary(glht(m1, linfct = c("treatment_pos + treatment_pos:priority_education_low==0")))
m2 <- lm(mayor_invested_healthcare~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m2_low <- summary(glht(m2, linfct = c("treatment_pos + treatment_pos:priority_education_low==0")))
m3 <- lm(mayor_invested_socialassistance~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m3_low <- summary(glht(m3, linfct = c("treatment_pos + treatment_pos:priority_education_low==0")))
m4 <- lm(mayor_invested_security~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m4_low <- summary(glht(m4, linfct = c("treatment_pos + treatment_pos:priority_education_low==0")))
m5 <- lm(mayor_improved_education~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m5_low <- summary(glht(m5, linfct = c("treatment_pos + treatment_pos:priority_education_low==0")))
m6 <- lm(mayor_improved_healthcare~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m6_low <- summary(glht(m6, linfct = c("treatment_pos + treatment_pos:priority_education_low==0")))
m7 <- lm(mayor_improved_socialassistance~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m7_low <- summary(glht(m7, linfct = c("treatment_pos + treatment_pos:priority_education_low==0")))
m8 <- lm(mayor_improved_security~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m8_low <- summary(glht(m8, linfct = c("treatment_pos + treatment_pos:priority_education_low==0")))

# Critical value and outcomes
critval_pos_95 <- qt(0.975, m8$df.residual)
areas <- rev(rep(c("Education", "Healthcare", "Social \n Assistance", "Security"),2))
focus <- rev(c(rep(c("Investments"),4), rep(c("Quality"),4)))
statements <- rev(c("The mayor invested \n in education", "The mayor invested \n in healthcare", "The mayor invested \n in social assistance", "The mayor invested \n in security",
                    "The mayor improved the \n quality of education", "The mayor improved the \n quality of healthcare", "The mayor improved the \n quality of social assistance", "The mayor improved the \n quality of security"))
# Treatmen effects for the group that gives high priority to education
betas <- c(m8$coefficients[["treatment_pos"]], m7$coefficients[["treatment_pos"]], m6$coefficients[["treatment_pos"]], m5$coefficients[["treatment_pos"]],m4$coefficients[["treatment_pos"]], m3$coefficients[["treatment_pos"]], m2$coefficients[["treatment_pos"]],  m1$coefficients[["treatment_pos"]])
ses <- c(coeftest(m8, type="HC2")[,2][["treatment_pos"]],coeftest(m7, type="HC2")[,2][["treatment_pos"]],coeftest(m6, type="HC2")[,2][["treatment_pos"]],coeftest(m5, type="HC2")[,2][["treatment_pos"]],coeftest(m4, type="HC2")[,2][["treatment_pos"]],coeftest(m3, type="HC2")[,2][["treatment_pos"]],coeftest(m2, type="HC2")[,2][["treatment_pos"]],coeftest(m1, type="HC2")[,2][["treatment_pos"]])
results_pos_high <- as.data.frame(cbind(areas, focus, statements, betas, ses))
results_pos_high$betas <- as.numeric(as.character(results_pos_high$betas))
results_pos_high$ses <- as.numeric(as.character(results_pos_high$ses))
results_pos_high$cilow_95 <- results_pos_high$betas - results_pos_high$ses*critval_pos_95
results_pos_high$ciup_95 <- results_pos_high$betas + critval_pos_95*results_pos_high$ses
# Treatment effects for the group that gives low priority to education
betas <- c(m8_low$test$coefficients[[1]], m7_low$test$coefficients[[1]], m6_low$test$coefficients[[1]], m5_low$test$coefficients[[1]], m4_low$test$coefficients[[1]], m3_low$test$coefficients[[1]], m2_low$test$coefficients[[1]], m1_low$test$coefficients[[1]])
ses <- c(m8_low$test$sigma[[1]], m7_low$test$sigma[[1]], m6_low$test$sigma[[1]], m5_low$test$sigma[[1]], m4_low$test$sigma[[1]], m3_low$test$sigma[[1]], m2_low$test$sigma[[1]], m1_low$test$sigma[[1]])
results_pos_low <- as.data.frame(cbind(areas, focus, statements, betas, ses))
results_pos_low$betas <- as.numeric(as.character(results_pos_low$betas))
results_pos_low$ses <- as.numeric(as.character(results_pos_low$ses))
results_pos_low$cilow_95 <- results_pos_low$betas - results_pos_low$ses*critval_pos_95
results_pos_low$ciup_95 <- results_pos_low$betas + critval_pos_95*results_pos_low$ses

pdf("figures/positive_treatment_conditional.pdf",width=9, height=5)
par(mfrow=c(1,2))
# Subplot in the left: Investments
par(mar=c(7,6,5,1))
plot(NULL, ylim = c(0.5,4.5), xlim = c(-0.4, 0.4), yaxt = "n",
     xlab = "CATE with 95% confidence interval", ylab = "", cex.main = 1.2,
     main = "CATE on beliefs that the mayor \n invested a lot of money in...")
grid()
abline(v=0, col="red", lty=1,lwd=1)
segments(y0=c(0.95:3.95),x0 = results_pos_high$cilow_95[5:8], x1 = results_pos_high$ciup_95[5:8],
         col = "blue", lwd = 1.5, lty=1)
points(y = c(0.95:3.95), x = results_pos_high$betas[5:8], pch = 17, cex=1.5, col="blue")
segments(y0=c(1.05:4.05),x0 = results_pos_low$cilow_95[5:8], x1 = results_pos_low$ciup_95[5:8],
         col = "blue", lwd = 1.5)
points(y = c(1.05:4.05), x = results_pos_low$betas[5:8], pch = 18, col="blue", cex=1.5)
axis(2, at=c(1:4),labels=results_pos_high$areas[1:4], las=2)
# Subplot in the right: Improvements in quality
par(mar=c(7,1,5,6))
plot(NULL, ylim = c(0.5,4.5), xlim = c(-0.4, 0.4), yaxt = "n",
     xlab = "CATE with 95% confidence interval", ylab = "", cex.main = 1.2,
     main = "CATE on beliefs that the mayor \n improved the quality of...")
grid()
abline(v=0, col="red",lty=1,lwd=1)
segments(y0=c(0.95:3.95),x0 = results_pos_high$cilow_95[1:4], x1 = results_pos_high$ciup_95[1:4],
         col = "blue", lwd = 1.5, lty=1)
points(y = c(0.95:3.95), x = results_pos_high$betas[1:4], pch = 17, cex=1.5, col="blue")
segments(y0=c(1.05:4.05),x0 = results_pos_low$cilow_95[1:4], x1 = results_pos_low$ciup_95[1:4],
         col = "blue", lwd = 1.5)
points(y = c(1.05:4.05), x = results_pos_low$betas[1:4], pch = 18, col="blue", cex=1.5)
axis(4, at=c(1:4),labels=results_pos_high$areas[1:4], las=2)
legend(-1,-1, col=c("blue","blue"), pch=c(17,18), cex=1, lwd=2, c("Positive treatment | Education more valued", "Positive treatment | Education less valued"),bty="n",xpd="NA")
dev.off()

# APPENDIX C2: COVARIATE BALANCE ------------------------------------------
covariates <- c("region_north", "region_centerwest", "region_southeast", "region_south",
                "priority_education", "priority_healthcare", "priority_economy",
                "priority_socialassistance", "priority_security", "had_heard_of_ideb",
                "age", "female", "race_white", "race_brown", "race_other", "education")
formula <- f.build("treatment", covariates)
# Balance, all respondents considered
db <- dd[,c("treatment",covariates)]
mb.all <- MatchBalance(formula, data = db)
mb_all <- baltest.collect(mb.all, var.names = covariates, after = F) %>%
  .[,c(1,2,3,6)]
colnames(mb_all) <- c("Mean treatment", "Mean control", "SD", "p value")
print(xtable(mb_all, 
             digits=3,
             caption = c("Covariate balance for the online survey experiment: All respondents"), 
             label=c("tab:facebook_balance_all")),
      include.rownames=T,
      file="tables/survey_balance_all.tex")
# Balance among respondents in the positive group (IDEB target met)
db_pos <- dd[dd$ideb==1,c("treatment",covariates)]
mb.pos <- MatchBalance(formula, data = db_pos)
mb_pos <- baltest.collect(mb.pos, var.names = covariates, after = F) %>%
  .[,c(1,2,3,6)]
colnames(mb_pos) <- c("Mean treatment", "Mean control", "SD", "p value")
print(xtable(mb_pos, 
             digits=3,
             caption = c("Covariate balance for the online survey experiment: Respondents in municipalities that had met their IDEB target"), 
             label=c("tab:facebook_balance_pos")),
      include.rownames=T,
      file="tables/survey_balance_pos.tex")
# Balance among respondents in the negative group (IDEB target missed)
db_neg <- dd[dd$ideb==0,c("treatment",covariates)]
mb.neg <- MatchBalance(formula, data = db_neg)
mb_neg <- baltest.collect(mb.neg, var.names = covariates, after = F) %>%
  .[,c(1,2,3,6)]
colnames(mb_neg) <- c("Mean treatment", "Mean control", "SD", "p value")
print(xtable(mb_neg, 
       digits=3,
       caption = c("Covariate balance for the online survey experiment: Respondents in municipalities that had not met their IDEB target"), 
       label=c("tab:facebook_balance_neg")),
       include.rownames=T,
       file="tables/survey_balance_neg.tex")

# APPENDIX C3: POLICY RANKING ---------------------------------------------
mean_ranking <- c(mean(dd$priority_education), mean(dd$priority_healthcare), mean(dd$priority_economy), 
                  mean(dd$priority_security), mean(dd$priority_socialassistance))
areas <- c("Education", "Healthcare", "Economy", "Security", "Social assistance")
rankings <- as_tibble(cbind(areas,mean_ranking))
rankings <- rankings[order(rankings$mean_ranking),]

pdf("figures/policy_areas_mean_ranks.pdf", width=9, height=5)
barplot(height=as.numeric(rankings$mean_ranking), names.arg=rankings$areas, 
        ylim=c(0,5), ylab="Mean ranking given by respondents")
dev.off()

# APPENDIX C4: PRE-SPECIFIED REGRESSIONS ----------------------------------
# Table C20: Positive treatment
m1 <- lm(mayor_invested_education~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m2 <- lm(mayor_invested_healthcare~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m3 <- lm(mayor_invested_socialassistance~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m4 <- lm(mayor_invested_security~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m5 <- lm(mayor_improved_education~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m6 <- lm(mayor_improved_healthcare~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m7 <- lm(mayor_improved_socialassistance~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m8 <- lm(mayor_improved_security~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)

texreg(list(m1,m2,m3,m4,m5,m6,m7,m8),
       file="tables/survey_positive.tex", table=T, float.pos="htp", scalebox=.9,
       digits=3,
       override.se = list(coeftest(m1, type="HC2")[,2], coeftest(m2, type="HC2")[,2], coeftest(m3, type="HC2")[,2], coeftest(m4, type="HC2")[,2], coeftest(m5, type="HC2")[,2], coeftest(m6, type="HC2")[,2], coeftest(m7, type="HC2")[,2], coeftest(m8, type="HC2")[,2]),
       override.pvalues = list(coeftest(m1, type="HC2")[,4], coeftest(m2, type="HC2")[,4], coeftest(m3, type="HC2")[,4], coeftest(m4, type="HC2")[,4], coeftest(m5, type="HC2")[,4], coeftest(m6, type="HC2")[,4], coeftest(m7, type="HC2")[,4], coeftest(m8, type="HC2")[,4]),
       custom.header = list("The mayor invested a lot in:" = 1:4, "The mayor improved the quality of:" = 5:8),
       booktabs = T, use.packages=F,
       custom.model.names = c(rep(c("Education", "Healthcare", "Social Assist.", "Security"),2)),
       custom.coef.map = list("treatment_pos" = "Treatment positive", "(Intercept)" = "Constant"),
       custom.gof.names = c(NA, "Observations"),
       reorder.gof = c(2,1),
       include.adjrs = F,
       stars = c(0.01, 0.05, 0.1),
       caption="Survey experiment results: Positive treatment (individuals in municipalities that met their IDEB target). Controls omitted. The dependent variable is respondents' agreement with the statement 'How much do you agree with the following statements about the mayor?’on a 1-4 scale (1 = disagree completely, 4 = agree completely.",
       label="tab:survey_positive",
       custom.coef.names=c("Constant", NA))

# Table C21: Negative treatment
m1 <- lm(mayor_invested_education~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m2 <- lm(mayor_invested_healthcare~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m3 <- lm(mayor_invested_socialassistance~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m4 <- lm(mayor_invested_security~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m5 <- lm(mayor_improved_education~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m6 <- lm(mayor_improved_healthcare~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m7 <- lm(mayor_improved_socialassistance~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m8 <- lm(mayor_improved_security~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)

texreg(list(m1,m2,m3,m4,m5,m6,m7,m8),
       file="tables/survey_negative.tex", table=T, float.pos="htp", scalebox=.9,
       digits=3,
       override.se = list(coeftest(m1, type="HC2")[,2], coeftest(m2, type="HC2")[,2], coeftest(m3, type="HC2")[,2], coeftest(m4, type="HC2")[,2], coeftest(m5, type="HC2")[,2], coeftest(m6, type="HC2")[,2], coeftest(m7, type="HC2")[,2], coeftest(m8, type="HC2")[,2]),
       override.pvalues = list(coeftest(m1, type="HC2")[,4], coeftest(m2, type="HC2")[,4], coeftest(m3, type="HC2")[,4], coeftest(m4, type="HC2")[,4], coeftest(m5, type="HC2")[,4], coeftest(m6, type="HC2")[,4], coeftest(m7, type="HC2")[,4], coeftest(m8, type="HC2")[,4]),
       custom.header = list("The mayor invested a lot in:" = 1:4, "The mayor improved the quality of:" = 5:8),
       booktabs = T, use.packages=F,
       custom.model.names = c(rep(c("Education", "Healthcare", "Social Assist.", "Security"),2)),
       custom.coef.map = list("treatment_neg" = "Treatment negative", "(Intercept)" = "Constant"),
       custom.gof.names = c(NA, "Observations"),
       reorder.gof = c(2,1),
       include.adjrs = F,
       stars = c(0.01, 0.05, 0.1),
       caption="Survey experiment results: Negative treatment (individuals in municipalities that did not meet their IDEB target). Controls omitted. The dependent variable is respondents' agreement with the statement 'How much do you agree with the following statements about the mayor?’on a 1-4 scale (1 = disagree completely, 4 = agree completely.",
       label="tab:survey_negative")

# Table C22: Positive treatment, heterogeneity by priority given to education
m1 <- lm(mayor_invested_education~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m2 <- lm(mayor_invested_healthcare~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m3 <- lm(mayor_invested_socialassistance~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m4 <- lm(mayor_invested_security~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m5 <- lm(mayor_improved_education~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m6 <- lm(mayor_improved_healthcare~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m7 <- lm(mayor_improved_socialassistance~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m8 <- lm(mayor_improved_security~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)

texreg(list(m1,m2,m3,m4,m5,m6,m7,m8),
       file="tables/survey_positive_heterogeneous.tex", table=T, float.pos="htp", scalebox=.9,
       digits=3,
       override.se = list(coeftest(m1, type="HC2")[,2], coeftest(m2, type="HC2")[,2], coeftest(m3, type="HC2")[,2], coeftest(m4, type="HC2")[,2], coeftest(m5, type="HC2")[,2], coeftest(m6, type="HC2")[,2], coeftest(m7, type="HC2")[,2], coeftest(m8, type="HC2")[,2]),
       override.pvalues = list(coeftest(m1, type="HC2")[,4], coeftest(m2, type="HC2")[,4], coeftest(m3, type="HC2")[,4], coeftest(m4, type="HC2")[,4], coeftest(m5, type="HC2")[,4], coeftest(m6, type="HC2")[,4], coeftest(m7, type="HC2")[,4], coeftest(m8, type="HC2")[,4]),
       custom.header = list("The mayor invested a lot in:" = 1:4, "The mayor improved the quality of:" = 5:8),
       booktabs = T, use.packages=F,
       custom.model.names = c(rep(c("Education", "Healthcare", "Social Assist.", "Security"),2)),
       custom.coef.map = list("treatment_pos" = "Treatment positive", "treatment_pos:priority_education_low" = "Tr. pos. x Ed. low priority", "priority_education_low" = "Education low priority", "(Intercept)" = "Constant"),
       custom.gof.names = c(NA, "Observations"),
       reorder.gof = c(2,1),
       include.adjrs = F,
       stars = c(0.01, 0.05, 0.1),
       caption="Survey experiment results: Positive treatment (individuals in municipalities that met their IDEB target), by whether they give high or low priority to education. 'Education low priority' is an indicator for whether respondents give below-median priority to education. Controls omitted. The dependent variable is respondents' agreement with the statement 'How much do you agree with the following statements about the mayor?’on a 1-4 scale (1 = disagree completely, 4 = agree completely.",
       label="tab:survey_positive_heterogeneous")

# Table C23: Negative treatment, heterogeneity by priority given to education
m1 <- lm(mayor_invested_education~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m2 <- lm(mayor_invested_healthcare~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m3 <- lm(mayor_invested_socialassistance~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m4 <- lm(mayor_invested_security~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m5 <- lm(mayor_improved_education~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m6 <- lm(mayor_improved_healthcare~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m7 <- lm(mayor_improved_socialassistance~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m8 <- lm(mayor_improved_security~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)

texreg(list(m1,m2,m3,m4,m5,m6,m7,m8),
       file="tables/survey_negative_heterogeneous.tex",table=T, float.pos="htp", scalebox=.9,
       digits=3,
       override.se = list(coeftest(m1, type="HC2")[,2], coeftest(m2, type="HC2")[,2], coeftest(m3, type="HC2")[,2], coeftest(m4, type="HC2")[,2], coeftest(m5, type="HC2")[,2], coeftest(m6, type="HC2")[,2], coeftest(m7, type="HC2")[,2], coeftest(m8, type="HC2")[,2]),
       override.pvalues = list(coeftest(m1, type="HC2")[,4], coeftest(m2, type="HC2")[,4], coeftest(m3, type="HC2")[,4], coeftest(m4, type="HC2")[,4], coeftest(m5, type="HC2")[,4], coeftest(m6, type="HC2")[,4], coeftest(m7, type="HC2")[,4], coeftest(m8, type="HC2")[,4]),
       custom.header = list("The mayor invested a lot in:" = 1:4, "The mayor improved the quality of:" = 5:8),
       booktabs = T, use.packages=F,
       custom.model.names = c(rep(c("Education", "Healthcare", "Social Assist.", "Security"),2)),
       custom.coef.map = list("treatment_neg" = "Treatment negative", "treatment_neg:priority_education_low" = "Tr. neg. x Ed. low priority", "priority_education_low" = "Education low priority", "(Intercept)" = "Constant"),
       custom.gof.names = c(NA, "Observations"),
       reorder.gof = c(2,1),
       include.adjrs = F,
       stars = c(0.01, 0.05, 0.1),
       caption="Survey experiment results: Negative treatment (individuals in municipalities that did not meet their IDEB target), by whether they give high or low priority to education. 'Education low priority' is an indicator for whether respondents give below-median priority to education. Controls omitted. The dependent variable is respondents' agreement with the statement 'How much do you agree with the following statements about the mayor?’on a 1-4 scale (1 = disagree completely, 4 = agree completely.",
       label="tab:survey_negative_heterogeneous")

# APPENDIX C5: REWEIGHTED REGRESSIONS ----------------------------------------------------------
# Table C24: Positive treatment, reweighted
m1 <- lm(mayor_invested_education~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m2 <- lm(mayor_invested_healthcare~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m3 <- lm(mayor_invested_socialassistance~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m4 <- lm(mayor_invested_security~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m5 <- lm(mayor_improved_education~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m6 <- lm(mayor_improved_healthcare~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m7 <- lm(mayor_improved_socialassistance~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m8 <- lm(mayor_improved_security~(treatment_pos)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)

texreg(list(m1,m2,m3,m4,m5,m6,m7,m8),
       file="tables/survey_positive_reweighted.tex", table=T, float.pos="htp", scalebox=.9,
       digits=3,
       override.se = list(coeftest(m1, type="HC2")[,2], coeftest(m2, type="HC2")[,2], coeftest(m3, type="HC2")[,2], coeftest(m4, type="HC2")[,2], coeftest(m5, type="HC2")[,2], coeftest(m6, type="HC2")[,2], coeftest(m7, type="HC2")[,2], coeftest(m8, type="HC2")[,2]),
       override.pvalues = list(coeftest(m1, type="HC2")[,4], coeftest(m2, type="HC2")[,4], coeftest(m3, type="HC2")[,4], coeftest(m4, type="HC2")[,4], coeftest(m5, type="HC2")[,4], coeftest(m6, type="HC2")[,4], coeftest(m7, type="HC2")[,4], coeftest(m8, type="HC2")[,4]),
       custom.header = list("The mayor invested a lot in:" = 1:4, "The mayor improved the quality of:" = 5:8),
       booktabs = T, use.packages=F,
       custom.model.names = c(rep(c("Education", "Healthcare", "Social Assist.", "Security"),2)),
       custom.coef.map = list("treatment_pos" = "Treatment positive", "(Intercept)" = "Constant"),
       custom.gof.names = c(NA, "Observations"),
       reorder.gof = c(2,1),
       include.adjrs = F,
       stars = c(0.01, 0.05, 0.1),
       caption="Survey experiment results: Positive treatment (individuals in municipalities that met their IDEB target). Controls omitted. The dependent variable is respondents' agreement with the statement 'How much do you agree with the following statements about the mayor?’on a 1-4 scale (1 = disagree completely, 4 = agree completely. Data reweighted by education level according to census figures for municipalities within the RDD bandwidth (column 1 in Table D.31).",
       label="tab:survey_positive_reweighted",
       custom.coef.names=c("Constant", NA))

# Table C25: Negative treatment, reweighted
m1 <- lm(mayor_invested_education~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m2 <- lm(mayor_invested_healthcare~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m3 <- lm(mayor_invested_socialassistance~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m4 <- lm(mayor_invested_security~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m5 <- lm(mayor_improved_education~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m6 <- lm(mayor_improved_healthcare~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m7 <- lm(mayor_improved_socialassistance~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m8 <- lm(mayor_improved_security~(treatment_neg)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)

texreg(list(m1,m2,m3,m4,m5,m6,m7,m8),
       file="tables/survey_negative_reweighted.tex", table=T, float.pos="htp", scalebox=.9,
       digits=3,
       override.se = list(coeftest(m1, type="HC2")[,2], coeftest(m2, type="HC2")[,2], coeftest(m3, type="HC2")[,2], coeftest(m4, type="HC2")[,2], coeftest(m5, type="HC2")[,2], coeftest(m6, type="HC2")[,2], coeftest(m7, type="HC2")[,2], coeftest(m8, type="HC2")[,2]),
       override.pvalues = list(coeftest(m1, type="HC2")[,4], coeftest(m2, type="HC2")[,4], coeftest(m3, type="HC2")[,4], coeftest(m4, type="HC2")[,4], coeftest(m5, type="HC2")[,4], coeftest(m6, type="HC2")[,4], coeftest(m7, type="HC2")[,4], coeftest(m8, type="HC2")[,4]),
       custom.header = list("The mayor invested a lot in:" = 1:4, "The mayor improved the quality of:" = 5:8),
       booktabs = T, use.packages=F,
       custom.model.names = c(rep(c("Education", "Healthcare", "Social Assist.", "Security"),2)),
       custom.coef.map = list("treatment_neg" = "Treatment negative", "(Intercept)" = "Constant"),
       custom.gof.names = c(NA, "Observations"),
       reorder.gof = c(2,1),
       include.adjrs = F,
       stars = c(0.01, 0.05, 0.1),
       caption="Survey experiment results: Negative treatment (individuals in municipalities that did not meet their IDEB target). Controls omitted. The dependent variable is respondents' agreement with the statement 'How much do you agree with the following statements about the mayor?’on a 1-4 scale (1 = disagree completely, 4 = agree completely. Data reweighted by education level according to census figures for municipalities within the RDD bandwidth (column 1 in Table D.31).",
       label="tab:survey_negative")

# Table C26: Positive treatment, reweighted, heterogeneity by priority given to education
m1 <- lm(mayor_invested_education~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m2 <- lm(mayor_invested_healthcare~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m3 <- lm(mayor_invested_socialassistance~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m4 <- lm(mayor_invested_security~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m5 <- lm(mayor_improved_education~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m6 <- lm(mayor_improved_healthcare~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m7 <- lm(mayor_improved_socialassistance~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)
m8 <- lm(mayor_improved_security~(treatment_pos*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1,weights=dd$weights_census_rdd)

texreg(list(m1,m2,m3,m4,m5,m6,m7,m8),
       file="tables/survey_positive_heterogeneous_reweighted.tex", table=T, float.pos="htp", scalebox=.9,
       digits=3,
       override.se = list(coeftest(m1, type="HC2")[,2], coeftest(m2, type="HC2")[,2], coeftest(m3, type="HC2")[,2], coeftest(m4, type="HC2")[,2], coeftest(m5, type="HC2")[,2], coeftest(m6, type="HC2")[,2], coeftest(m7, type="HC2")[,2], coeftest(m8, type="HC2")[,2]),
       override.pvalues = list(coeftest(m1, type="HC2")[,4], coeftest(m2, type="HC2")[,4], coeftest(m3, type="HC2")[,4], coeftest(m4, type="HC2")[,4], coeftest(m5, type="HC2")[,4], coeftest(m6, type="HC2")[,4], coeftest(m7, type="HC2")[,4], coeftest(m8, type="HC2")[,4]),
       custom.header = list("The mayor invested a lot in:" = 1:4, "The mayor improved the quality of:" = 5:8),
       booktabs = T, use.packages=F,
       custom.model.names = c(rep(c("Education", "Healthcare", "Social Assist.", "Security"),2)),
       custom.coef.map = list("treatment_pos" = "Treatment positive", "treatment_pos:priority_education_low" = "Tr. pos. x Ed. low priority", "priority_education_low" = "Education low priority", "(Intercept)" = "Constant"),
       custom.gof.names = c(NA, "Observations"),
       reorder.gof = c(2,1),
       include.adjrs = F,
       stars = c(0.01, 0.05, 0.1),
       caption="Survey experiment results: Positive treatment (individuals in municipalities that met their IDEB target), by whether they give high or low priority to education. 'Education low priority' is an indicator for whether respondents give below-median priority to education. Controls omitted. The dependent variable is respondents' agreement with the statement 'How much do you agree with the following statements about the mayor?’on a 1-4 scale (1 = disagree completely, 4 = agree completely. Data reweighted by education level according to census figures for municipalities within the RDD bandwidth (column 1 in Table D.31).",
       label="tab:survey_positive_heterogeneous_reweighted")

# Table C27: Negative treatment, reweighted, heterogeneity by priority given to education
m1 <- lm(mayor_invested_education~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m2 <- lm(mayor_invested_healthcare~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m3 <- lm(mayor_invested_socialassistance~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m4 <- lm(mayor_invested_security~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m5 <- lm(mayor_improved_education~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m6 <- lm(mayor_improved_healthcare~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m7 <- lm(mayor_improved_socialassistance~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)
m8 <- lm(mayor_improved_security~(treatment_neg*priority_education_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0,weights=dd$weights_census_rdd)

texreg(list(m1,m2,m3,m4,m5,m6,m7,m8),
       file="tables/survey_negative_heterogeneous_reweighted.tex",table=T, float.pos="htp", scalebox=.9,
       digits=3,
       override.se = list(coeftest(m1, type="HC2")[,2], coeftest(m2, type="HC2")[,2], coeftest(m3, type="HC2")[,2], coeftest(m4, type="HC2")[,2], coeftest(m5, type="HC2")[,2], coeftest(m6, type="HC2")[,2], coeftest(m7, type="HC2")[,2], coeftest(m8, type="HC2")[,2]),
       override.pvalues = list(coeftest(m1, type="HC2")[,4], coeftest(m2, type="HC2")[,4], coeftest(m3, type="HC2")[,4], coeftest(m4, type="HC2")[,4], coeftest(m5, type="HC2")[,4], coeftest(m6, type="HC2")[,4], coeftest(m7, type="HC2")[,4], coeftest(m8, type="HC2")[,4]),
       custom.header = list("The mayor invested a lot in:" = 1:4, "The mayor improved the quality of:" = 5:8),
       booktabs = T, use.packages=F,
       custom.model.names = c(rep(c("Education", "Healthcare", "Social Assist.", "Security"),2)),
       custom.coef.map = list("treatment_neg" = "Treatment negative", "treatment_neg:priority_education_low" = "Tr. neg. x Ed. low priority", "priority_education_low" = "Education low priority", "(Intercept)" = "Constant"),
       custom.gof.names = c(NA, "Observations"),
       reorder.gof = c(2,1),
       include.adjrs = F,
       stars = c(0.01, 0.05, 0.1),
       caption="Survey experiment results: Negative treatment (individuals in municipalities that did not meet their IDEB target), by whether they give high or low priority to education. 'Education low priority' is an indicator for whether respondents give below-median priority to education. Controls omitted. The dependent variable is respondents' agreement with the statement 'How much do you agree with the following statements about the mayor?’on a 1-4 scale (1 = disagree completely, 4 = agree completely.  Data reweighted by education level according to census figures for municipalities within the RDD bandwidth (column 1 in Table D.31).",
       label="tab:survey_negative_heterogeneous_reweighted")

# APPENDIX C6: REGRESSIONS WITH CHANGED DEFINITION OF LOW-PRIORITY GROUP ---------------------------------
# Table C28: Positive treatment, heterogeneity by priority given to education
m1 <- lm(mayor_invested_education~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m2 <- lm(mayor_invested_healthcare~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m3 <- lm(mayor_invested_socialassistance~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m4 <- lm(mayor_invested_security~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m5 <- lm(mayor_improved_education~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m6 <- lm(mayor_improved_healthcare~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m7 <- lm(mayor_improved_socialassistance~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m8 <- lm(mayor_improved_security~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)

texreg(list(m1,m2,m3,m4,m5,m6,m7,m8),
       file="tables/survey_positive_heterogeneous_verylow.tex", table=T, float.pos="htp", scalebox=.9,
       digits=3,
       override.se = list(coeftest(m1, type="HC2")[,2], coeftest(m2, type="HC2")[,2], coeftest(m3, type="HC2")[,2], coeftest(m4, type="HC2")[,2], coeftest(m5, type="HC2")[,2], coeftest(m6, type="HC2")[,2], coeftest(m7, type="HC2")[,2], coeftest(m8, type="HC2")[,2]),
       override.pvalues = list(coeftest(m1, type="HC2")[,4], coeftest(m2, type="HC2")[,4], coeftest(m3, type="HC2")[,4], coeftest(m4, type="HC2")[,4], coeftest(m5, type="HC2")[,4], coeftest(m6, type="HC2")[,4], coeftest(m7, type="HC2")[,4], coeftest(m8, type="HC2")[,4]),
       custom.header = list("The mayor invested a lot in:" = 1:4, "The mayor improved the quality of:" = 5:8),
       booktabs = T, use.packages=F,
       custom.model.names = c(rep(c("Education", "Healthcare", "Social Assist.", "Security"),2)),
       custom.coef.map = list("treatment_pos" = "Treatment positive", "treatment_pos:priority_education_very_low" = "Tr. pos. x Ed. v. low priority", "priority_education_very_low" = "Education very low priority", "(Intercept)" = "Constant"),
       custom.gof.names = c(NA, "Observations"),
       reorder.gof = c(2,1),
       include.adjrs = F,
       stars = c(0.01, 0.05, 0.1),
       caption="Survey experiment results: Positive treatment (individuals in municipalities that met their IDEB target), by whether they give high or low priority to education. 'Education very low priority' is an indicator for whether espondents’ place three or more policy areas as more important than education. Controls omitted. The dependent variable is respondents' agreement with the statement 'How much do you agree with the following statements about the mayor?’on a 1-4 scale (1 = disagree completely, 4 = agree completely.",
       label="tab:survey_positive_heterogeneous_verylow")

# Table C29: Positive treatment, heterogeneity by priority given to education
m1 <- lm(mayor_invested_education~(treatment_neg*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m2 <- lm(mayor_invested_healthcare~(treatment_neg*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m3 <- lm(mayor_invested_socialassistance~(treatment_neg*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m4 <- lm(mayor_invested_security~(treatment_neg*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m5 <- lm(mayor_improved_education~(treatment_neg*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m6 <- lm(mayor_improved_healthcare~(treatment_neg*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m7 <- lm(mayor_improved_socialassistance~(treatment_neg*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)
m8 <- lm(mayor_improved_security~(treatment_neg*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==0)

texreg(list(m1,m2,m3,m4,m5,m6,m7,m8),
       file="tables/survey_negative_heterogeneous_verylow.tex", table=T, float.pos="htp", scalebox=.9,
       digits=3,
       override.se = list(coeftest(m1, type="HC2")[,2], coeftest(m2, type="HC2")[,2], coeftest(m3, type="HC2")[,2], coeftest(m4, type="HC2")[,2], coeftest(m5, type="HC2")[,2], coeftest(m6, type="HC2")[,2], coeftest(m7, type="HC2")[,2], coeftest(m8, type="HC2")[,2]),
       override.pvalues = list(coeftest(m1, type="HC2")[,4], coeftest(m2, type="HC2")[,4], coeftest(m3, type="HC2")[,4], coeftest(m4, type="HC2")[,4], coeftest(m5, type="HC2")[,4], coeftest(m6, type="HC2")[,4], coeftest(m7, type="HC2")[,4], coeftest(m8, type="HC2")[,4]),
       custom.header = list("The mayor invested a lot in:" = 1:4, "The mayor improved the quality of:" = 5:8),
       booktabs = T, use.packages=F,
       custom.model.names = c(rep(c("Education", "Healthcare", "Social Assist.", "Security"),2)),
       custom.coef.map = list("treatment_neg" = "Treatment negative", "treatment_neg:priority_education_very_low" = "Tr. neg. x Ed. v. low priority", "priority_education_very_low" = "Education very low priority", "(Intercept)" = "Constant"),
       custom.gof.names = c(NA, "Observations"),
       reorder.gof = c(2,1),
       include.adjrs = F,
       stars = c(0.01, 0.05, 0.1),
       caption="Survey experiment results: Negative treatment (individuals in municipalities that did not meet their IDEB target), by whether they give high or low priority to education. 'Education very low priority' is an indicator for whether espondents’ place three or more policy areas as more important than education. Controls omitted. The dependent variable is respondents' agreement with the statement 'How much do you agree with the following statements about the mayor?’on a 1-4 scale (1 = disagree completely, 4 = agree completely.",
       label="tab:survey_negative_heterogeneous_verylow")

# Figure C13: Conditional Average Treatment Effects plots
## Regressions for positive treatment, heterogeneity by priority given to education (Table C.22)
m1 <- lm(mayor_invested_education~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m1_low <- summary(glht(m1, linfct = c("treatment_pos + treatment_pos:priority_education_very_low==0")))
m2 <- lm(mayor_invested_healthcare~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m2_low <- summary(glht(m2, linfct = c("treatment_pos + treatment_pos:priority_education_very_low==0")))
m3 <- lm(mayor_invested_socialassistance~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m3_low <- summary(glht(m3, linfct = c("treatment_pos + treatment_pos:priority_education_very_low==0")))
m4 <- lm(mayor_invested_security~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m4_low <- summary(glht(m4, linfct = c("treatment_pos + treatment_pos:priority_education_very_low==0")))
m5 <- lm(mayor_improved_education~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m5_low <- summary(glht(m5, linfct = c("treatment_pos + treatment_pos:priority_education_very_low==0")))
m6 <- lm(mayor_improved_healthcare~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m6_low <- summary(glht(m6, linfct = c("treatment_pos + treatment_pos:priority_education_very_low==0")))
m7 <- lm(mayor_improved_socialassistance~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m7_low <- summary(glht(m7, linfct = c("treatment_pos + treatment_pos:priority_education_very_low==0")))
m8 <- lm(mayor_improved_security~(treatment_pos*priority_education_very_low)*(age + female + education + race_white + race_brown + region_north + region_centerwest + region_southeast + region_south),data=dd, subset=dd$ideb==1)
m8_low <- summary(glht(m8, linfct = c("treatment_pos + treatment_pos:priority_education_very_low==0")))
## Critical value and outcomes
critval_pos_95 <- qt(0.975, m8$df.residual)
areas <- rev(rep(c("Education", "Healthcare", "Social \n Assistance", "Security"),2))
focus <- rev(c(rep(c("Investments"),4), rep(c("Quality"),4)))
statements <- rev(c("The mayor invested \n in education", "The mayor invested \n in healthcare", "The mayor invested \n in social assistance", "The mayor invested \n in security",
                    "The mayor improved the \n quality of education", "The mayor improved the \n quality of healthcare", "The mayor improved the \n quality of social assistance", "The mayor improved the \n quality of security"))
## Treatmen effects for the group that gives high priority to education
betas <- c(m8$coefficients[["treatment_pos"]], m7$coefficients[["treatment_pos"]], m6$coefficients[["treatment_pos"]], m5$coefficients[["treatment_pos"]],m4$coefficients[["treatment_pos"]], m3$coefficients[["treatment_pos"]], m2$coefficients[["treatment_pos"]],  m1$coefficients[["treatment_pos"]])
ses <- c(coeftest(m8, type="HC2")[,2][["treatment_pos"]],coeftest(m7, type="HC2")[,2][["treatment_pos"]],coeftest(m6, type="HC2")[,2][["treatment_pos"]],coeftest(m5, type="HC2")[,2][["treatment_pos"]],coeftest(m4, type="HC2")[,2][["treatment_pos"]],coeftest(m3, type="HC2")[,2][["treatment_pos"]],coeftest(m2, type="HC2")[,2][["treatment_pos"]],coeftest(m1, type="HC2")[,2][["treatment_pos"]])
results_pos_high <- as.data.frame(cbind(areas, focus, statements, betas, ses))
results_pos_high$betas <- as.numeric(as.character(results_pos_high$betas))
results_pos_high$ses <- as.numeric(as.character(results_pos_high$ses))
results_pos_high$cilow_95 <- results_pos_high$betas - results_pos_high$ses*critval_pos_95
results_pos_high$ciup_95 <- results_pos_high$betas + critval_pos_95*results_pos_high$ses
## Treatment effects for the group that gives low priority to education
betas <- c(m8_low$test$coefficients[[1]], m7_low$test$coefficients[[1]], m6_low$test$coefficients[[1]], m5_low$test$coefficients[[1]], m4_low$test$coefficients[[1]], m3_low$test$coefficients[[1]], m2_low$test$coefficients[[1]], m1_low$test$coefficients[[1]])
ses <- c(m8_low$test$sigma[[1]], m7_low$test$sigma[[1]], m6_low$test$sigma[[1]], m5_low$test$sigma[[1]], m4_low$test$sigma[[1]], m3_low$test$sigma[[1]], m2_low$test$sigma[[1]], m1_low$test$sigma[[1]])
results_pos_low <- as.data.frame(cbind(areas, focus, statements, betas, ses))
results_pos_low$betas <- as.numeric(as.character(results_pos_low$betas))
results_pos_low$ses <- as.numeric(as.character(results_pos_low$ses))
results_pos_low$cilow_95 <- results_pos_low$betas - results_pos_low$ses*critval_pos_95
results_pos_low$ciup_95 <- results_pos_low$betas + critval_pos_95*results_pos_low$ses

pdf("figures/positive_treatment_conditional_verylow.pdf",width=9, height=5)
par(mfrow=c(1,2))
# Subplot in the left: Investments
par(mar=c(7,6,5,1))
plot(NULL, ylim = c(0.5,4.5), xlim = c(-0.45, 0.4), yaxt = "n",
     xlab = "CATE with 95% confidence interval", ylab = "", cex.main = 1.2,
     main = "CATE on beliefs that the mayor \n invested a lot of money in...")
grid()
abline(v=0, col="red", lty=1,lwd=1)
segments(y0=c(0.95:3.95),x0 = results_pos_high$cilow_95[5:8], x1 = results_pos_high$ciup_95[5:8],
         col = "blue", lwd = 1.5, lty=1)
points(y = c(0.95:3.95), x = results_pos_high$betas[5:8], pch = 17, cex=1.5, col="blue")
segments(y0=c(1.05:4.05),x0 = results_pos_low$cilow_95[5:8], x1 = results_pos_low$ciup_95[5:8],
         col = "blue", lwd = 1.5)
points(y = c(1.05:4.05), x = results_pos_low$betas[5:8], pch = 18, col="blue", cex=1.5)
axis(2, at=c(1:4),labels=results_pos_high$areas[1:4], las=2)
# Subplot in the right: Improvements in quality
par(mar=c(7,1,5,6))
plot(NULL, ylim = c(0.5,4.5), xlim = c(-0.45, 0.4), yaxt = "n",
     xlab = "CATE with 95% confidence interval", ylab = "", cex.main = 1.2,
     main = "CATE on beliefs that the mayor \n improved the quality of...")
grid()
abline(v=0, col="red",lty=1,lwd=1)
segments(y0=c(0.95:3.95),x0 = results_pos_high$cilow_95[1:4], x1 = results_pos_high$ciup_95[1:4],
         col = "blue", lwd = 1.5, lty=1)
points(y = c(0.95:3.95), x = results_pos_high$betas[1:4], pch = 17, cex=1.5, col="blue")
segments(y0=c(1.05:4.05),x0 = results_pos_low$cilow_95[1:4], x1 = results_pos_low$ciup_95[1:4],
         col = "blue", lwd = 1.5)
points(y = c(1.05:4.05), x = results_pos_low$betas[1:4], pch = 18, col="blue", cex=1.5)
axis(4, at=c(1:4),labels=results_pos_high$areas[1:4], las=2)
legend(-1,-1, col=c("blue","blue"), pch=c(17,18), cex=1, lwd=2, c("Positive treatment | Education more valued", "Positive treatment | Education less valued"),bty="n",xpd="NA")
dev.off()

# NOTES -- R version, platform, and loaded packages -------------------------
# sessionInfo(package = NULL)
# R version 3.6.3 (2020-02-29)
# Platform: x86_64-apple-darwin15.6.0 (64-bit)
# Running under: macOS Catalina 10.15.3
# 
# Matrix products: default
# BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
# 
# Random number generation:
#   RNG:     Mersenne-Twister 
# Normal:  Inversion 
# Sample:  Rounding 
# 
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] cobalt_4.1.0      ebal_0.1-6        Matching_4.9-7    multcomp_1.4-13   TH.data_1.0-10   
# [6] MASS_7.3-51.5     mvtnorm_1.1-0     Hmisc_4.4-0       lattice_0.20-40   timelineS_0.1.1  
# [11] lfe_2.8-5         Matrix_1.2-18     xtable_1.8-4      texreg_1.37.1     rdrobust_0.99.7  
# [16] rdd_0.57          Formula_1.2-3     AER_1.2-9         survival_3.1-11   car_3.0-7        
# [21] carData_3.0-3     lmtest_0.9-37     zoo_1.8-7         sandwich_2.5-1    shiny_1.4.0.2    
# [26] readxl_1.3.1      codebook_0.9.2    electionsBR_0.3.1 forcats_0.5.0     stringr_1.4.0    
# [31] dplyr_1.0.0       purrr_0.3.3       readr_1.3.1       tidyr_1.0.2       tibble_3.0.0     
# [36] ggplot2_3.3.0     tidyverse_1.3.0  
# 
# loaded via a namespace (and not attached):
#   [1] colorspace_1.4-1    ellipsis_0.3.0      rio_0.5.16          htmlTable_1.13.3   
# [5] base64enc_0.1-3     fs_1.4.1            rstudioapi_0.11     listenv_0.8.0      
# [9] farver_2.0.3        DT_0.13             fansi_0.4.1         lubridate_1.7.4    
# [13] xml2_1.3.0          codetools_0.2-16    splines_3.6.3       knitr_1.28         
# [17] jsonlite_1.6.1      broom_0.5.5         cluster_2.1.0       dbplyr_1.4.2       
# [21] png_0.1-7           compiler_3.6.3      httr_1.4.1          backports_1.1.5    
# [25] assertthat_0.2.1    fastmap_1.0.1       cli_2.0.2           later_1.0.0        
# [29] acepack_1.4.1       htmltools_0.4.0     tools_3.6.3         gtable_0.3.0       
# [33] glue_1.3.2          Rcpp_1.0.4          cellranger_1.1.0    vctrs_0.3.1        
# [37] nlme_3.1-145        crosstalk_1.1.0.1   xfun_0.12           globals_0.12.5     
# [41] openxlsx_4.1.4      rvest_0.3.5         mime_0.9            miniUI_0.1.1.1     
# [45] lifecycle_0.2.0     future_1.17.0       scales_1.1.0        hms_0.5.3          
# [49] promises_1.1.0      parallel_3.6.3      RColorBrewer_1.1-2  yaml_2.2.1         
# [53] curl_4.3            gridExtra_2.3       rpart_4.1-15        labelled_2.5.0     
# [57] latticeExtra_0.6-29 stringi_1.4.6       highr_0.8           checkmate_2.0.0    
# [61] rmdpartials_0.5.8   zip_2.0.4           repr_1.1.0          rlang_0.4.6        
# [65] pkgconfig_2.0.3     evaluate_0.14       htmlwidgets_1.5.1   labeling_0.3       
# [69] tidyselect_1.1.0    magrittr_1.5        R6_2.4.1            generics_0.0.2     
# [73] DBI_1.1.0           pillar_1.4.3        haven_2.3.1         foreign_0.8-76     
# [77] withr_2.1.2         nnet_7.3-13         abind_1.4-5         modelr_0.1.6       
# [81] crayon_1.3.4        utf8_1.1.4          rmarkdown_2.1       jpeg_0.1-8.1       
# [85] grid_3.6.3          data.table_1.12.8   reprex_0.3.0        digest_0.6.25      
# [89] httpuv_1.5.2        munsell_0.5.0       skimr_2.1.1  